#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "user.h"
#include "usermapper.h"
#include <QList>
#include "sqlinjectionexception.h"


MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //testInsertOne();
    //testInsertBatch();
    //selectWhereTest();
    updateWhereTestById(); //更新测试，更新按照Id
    //updateWhereTestByName(); //通过name更新

    //deleteWhereTestById();


}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::testInsertOne()
{
    // QList<User> newUsers = {
    //     User("Alice", "alice@example.com", 25),
    //     User("Bob", "bob@example.com", 30),
    //     User("Charlie", "charlie@example.com", 28)
    // };
#if 0
    User user = User("Jimi", "jimi@example.com", 28);
    UserMapper userMapper ;
    if(userMapper.insert(user))
    {
        qDebug() << "单个插入成功：" << Qt::endl;
    }
#endif
#if 0
    User user= User(6,"lixoaoming","lixiaoming@example.email",32); //当有自增长列不能赋值
    UserMapper userMapper ;
    try{
        if(userMapper.insert(user))
        {
            qDebug() << "单个插入成功：" << Qt::endl;
        }
    }catch(SqlInjectionException ex)
    {
        qDebug() << ex.what() << Qt::endl;

    }
#endif
    User user= User("zhangsan","zhangsan@example.email"); //当有自增长列不能赋值
    UserMapper userMapper ;
    try{
        if(userMapper.insert(user))
        {
            qDebug() << "单个插入成功：" << Qt::endl;
        }
    }catch(SqlInjectionException ex)
    {
        qDebug() << ex.what() << Qt::endl;

    }
}

void MainWindow::testInsertBatch()
{
    QList<User> newUsers = {
        User("Alice", "alice@example.com", 25),
        User("Bob", "bob@example.com", 30),
        User("Charlie", "charlie@example.com", 28)
    };
    UserMapper usermapper;
    if(usermapper.insertBatch(newUsers))
    {
        qDebug() << __LINE__ << "批量插入成功：" << Qt::endl;
    }
}

void MainWindow::selectWhereTest()
{
    UserMapper userMapper ;
#if 0
    QList<User> users = userMapper.selectAdults();
    foreach (User user, users) {
        qDebug() << user.getName() << user.getEmail() << user.getAge() << Qt::endl;
    }
#endif
    QtMyBatisPlus::QueryWrapper wrapper ;
    wrapper.eq("name","zhangsan") ;
    QList<User> users = userMapper.selectList(wrapper) ;
    foreach (User user, users) {
        qDebug() << __LINE__  << user.getId() << user.getName() << user.getEmail() <<  user.getCreatedTime() << Qt::endl;
        QVariant userAge = user.getAge() ;
        if(userAge.isNull())
        {
            qDebug() << __LINE__<< "age is null" << Qt:: endl;
        }else{
            qDebug() <<__LINE__ << "age is "  << userAge.toInt() << Qt::endl; //解决了
        }
    }
}
void MainWindow::updateWhereTestById()
{
    QtMyBatisPlus::QueryWrapper wrapper;
    UserMapper userMapper;
    User user = User(11,"AliceBrother", "AliceBrother2@example.com",39); //发生了更新，但是
    //QDateTime time =  user.getCreatedTime();
    // if(!time.isValid()){
    //     qDebug() << __LINE__ << Qt::endl;
    // }
    bool flag = userMapper.updateById(user);
}

void MainWindow::updateWhereTestByName()
{
    QtMyBatisPlus::QueryWrapper wrapper;
    UserMapper userMapper;
    User user = User(14,"AliceSister1", "AliceSister6@example.com",32); //发生了更新，但是
    bool flag = userMapper.updateByWrapper(user,wrapper.eq("name","Alice"));
    if(flag)
    {
        qDebug() << __LINE__ << flag << Qt::endl;
    }
}

void MainWindow::deleteWhereTestById()
{
    UserMapper userMapper;
    QtMyBatisPlus::QueryWrapper wrapper;
    wrapper.eq("id",6);
    bool flag = userMapper.deleteByWrapper(wrapper);
    if(flag)
    {
        qDebug() << __LINE__ << flag << Qt::endl;
    }

}





